using System;
using System.Data.Entity;
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using Microsoft.Practices.Unity;
using QuickPrompt.BusinessService;
using QuickPrompt.DataAccess;
using QuickPrompt.DomainModel;
using QuickPrompt.DomainModel.Identity;
using QuickPrompt.Spa.Controllers;
using QuickPromt.Service;
using RedandBlue.RepositoryPattern.DataContext;
using RedandBlue.RepositoryPattern.EF6;
using RedandBlue.RepositoryPattern.Repositories;
using RedandBlue.RepositoryPattern.UnitOfWork;

namespace QuickPrompt.Spa
{
    /// <summary>
    /// Specifies the Unity configuration for the main container.
    /// </summary>
    public class UnityConfig
    {
        #region Unity Container
        private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            return container;
        });

        /// <summary>
        /// Gets the configured Unity container.
        /// </summary>
        public static IUnityContainer GetConfiguredContainer()
        {
            return container.Value;
        }
        #endregion

        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            // container.LoadConfiguration();

            // TODO: Register your types here
            // container.RegisterType<IProductRepository, ProductRepository>();
            container.RegisterType<IUserStore<User>, UserStore<User>>(new HierarchicalLifetimeManager());
            container.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication));
            container.RegisterType<AccountController>(new InjectionConstructor());
            container.RegisterType<UserManager<User>>(new HierarchicalLifetimeManager());
            container.RegisterType<DbContext, QuickPromptContext>(new HierarchicalLifetimeManager());
            container.RegisterType<IDataContextAsync, QuickPromptContext>();
            container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new InjectionConstructor(typeof(IDataContextAsync)));
            container.RegisterType(typeof (IRepositoryAsync<>), typeof (Repository<>));
            container.RegisterType<IUserService, UserService>();
            container.RegisterType<IQuickNoteService, QuickNoteService>();
            container.RegisterType<IQuickPromptService, QuickPromptService>();
        }
    }
}
